EXEC sp_configure 'clr enable',1

GO

RECONFIGURE

GO

USE wsdb_db

GO

EXEC sp_changedbowner 'sa'

GO

ALTER DATABASE wsdb_db SET TRUSTWORTHY ON

GO

CREATE ASSEMBLY db_wsdb
FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C010300B4844C560000000000000000E00002210B010B00000800000006000000000000EE260000002000000040000000000010002000000002000004000000000000000600000000000000008000000002000000000000030060850000100000100000000010000010000000000000100000000000000000000000982600005300000000400000A002000000000000000000000000000000000000006000000C000000602500001C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000F4060000002000000008000000020000000000000000000000000000200000602E72737263000000A00200000040000000040000000A0000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000000E00000000000000000000000000004000004200000000000000000000000000000000D0260000000000004800000002000500D42000008C0400000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000133002006E000000010000110072010000700F00FE16030000016F0500000A280600000A0A140B061201280700000A0C08130411042D0A0015280800000A0D2B370714FE0116FE01130411042D0A0015280800000A0D2B20076F0900000A26076F0A00000A00076F0B00000A00140B17280800000A0D2B00092A1E02280C00000A2A000042534A4201000100000000000C00000076342E302E33303331390000000005006C0000006C010000237E0000D8010000CC01000023537472696E677300000000A40300005C0000002355530000040000100000002347554944000000100400007C00000023426C6F620000000000000002000001471502000900000000FA253300160000010000000B0000000200000002000000010000000C0000000400000001000000010000000200000000000A000100000000000600370030000A005F004A000A0068004A0006009E008B001300B20000000600E100C10006000101C1000A0042012701060061013000060080016F010600B0016F0100000000010000000000010001000100100016000000050001000100502000000000960072000A000100CA200000000086187E001100020000000100840021007E00150031007E001B0039007E00110041007E001100090058012500490068012900510090012F001100A00137005100AC013D005900BB0111005900C101110009007E0011002000230020002E000B004B002E00130054002E001B005D0041000480000000000000000000000000000000001F010000040000000000000000000000010027000000000004000000000000000000000001003E00000000000000003C4D6F64756C653E0064625F777364622E646C6C0053746F72656450726F63656475726573006D73636F726C69620053797374656D004F626A6563740053797374656D2E446174610053797374656D2E446174612E53716C54797065730053716C496E7433320053716C537472696E6700575344425F4E4F54494659002E63746F7200736368656D610053797374656D2E446961676E6F73746963730044656275676761626C6541747472696275746500446562756767696E674D6F6465730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C6974794174747269627574650064625F77736462004D6963726F736F66742E53716C5365727665722E5365727665720053716C50726F63656475726541747472696275746500546F537472696E6700537472696E6700436F6E6361740053797374656D2E546872656164696E67004576656E745761697448616E646C65005472794F70656E4578697374696E67006F705F496D706C6963697400536574005761697448616E646C6500436C6F736500446973706F736500000000005947006C006F00620061006C005C00460036003000440046004400330038002D0037003800370036002D0034003200390041002D0038003900390030002D003600340036003200310038003300410042003800460034005F000100B9BED18E8523DA40A16A08777F0DC43A0008B77A5C561934E0890600011109110D03200001052001011115042001010804010000000320000E0500020E0E0E070002020E101229050001110908032000020907050E1229021109020801000701000000000801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F77730100000000B4844C5600000000020000001C0100007C2500007C07000052534453C3C6ED37E6F1104A9F949AB7359C3A9B03000000633A5C55736572735C4D415254594E41535C4F6E6544726976655C4461726261695C7A6D616E6F5C777364625C64625F777364625C6F626A5C44656275675C64625F777364622E706462000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C02600000000000000000000DE260000002000000000000000000000000000000000000000000000D026000000000000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF25002000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100000001800008000000000000000000000000000000100010000003000008000000000000000000000000000000100000000004800000058400000440200000000000000000000440234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004A4010000010053007400720069006E006700460069006C00650049006E0066006F0000008001000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E003000000038000C00010049006E007400650072006E0061006C004E0061006D0065000000640062005F0077007300640062002E0064006C006C0000002800020001004C006500670061006C0043006F00700079007200690067006800740000002000000040000C0001004F0072006900670069006E0061006C00460069006C0065006E0061006D0065000000640062005F0077007300640062002E0064006C006C000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E00300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000C000000F03600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
WITH PERMISSION_SET = UNSAFE

GO

CREATE PROCEDURE dbo.WSDB_NOTIFY
	@schema nvarchar(max)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME db_wsdb.StoredProcedures.WSDB_NOTIFY

GO

CREATE SCHEMA z

GO

CREATE PROCEDURE z.WSDB_NOTIFY
(@ddlxml xml)
WITH EXECUTE AS OWNER
AS

SET NOCOUNT ON
SET TEXTSIZE 2147483647

DECLARE @et nvarchar(128)
DECLARE @un nvarchar(128)
DECLARE @db nvarchaR(128)
DECLARE @sn nvarchar(128)
DECLARE @on nvarchar(128)
DECLARE @ot nvarchar(128)
DECLARE @tsql nvarchar(max)
DECLARE @login nvarchar(128)
DECLARE @dt datetime

SELECT
  @et = @ddlxml.value('(/EVENT_INSTANCE/EventType)[1]'    , 'nvarchar(128)'),         
  @sn = @ddlxml.value('(/EVENT_INSTANCE/SchemaName)[1]'   , 'nvarchar(128)'),
  @on = @ddlxml.value('(/EVENT_INSTANCE/ObjectName)[1]'   , 'nvarchar(128)'),
  @tsql = @ddlxml.value('(/EVENT_INSTANCE/TSQLCommand)[1]'  , 'nvarchar(max)'),
  @db = @ddlxml.value('(/EVENT_INSTANCE/DatabaseName)[1]' , 'nvarchar(128)'),
  @un = @ddlxml.value('(/EVENT_INSTANCE/UserName)[1]'     , 'nvarchar(128)'),
  @ot = @ddlxml.value('(/STANCE/ObjectType)[1]'   , 'nvarchar(128)'),
  @login = @ddlxml.value('(/EVENT_INSTANCE/LoginName)[1]'    , 'nvarchar(128)'),
  @dt = @ddlxml.value('(/EVENT_INSTANCE/PostTime)[1]'     , 'datetime')

IF @ot='PROCEDURE' BEGIN
  IF @sn='ws' BEGIN
    EXEC dbo.WSDB_NOTIFY 'ws'
  END
END

GO

CREATE TRIGGER TRGDB_SYS
ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS
AS

SET NOCOUNT ON

DECLARE @ddl xml

SET @ddl = EVENTDATA()

EXEC z.WSDB_NOTIFY @ddl

SET NOCOUNT OFF
